package org.example.network1.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class Client01 {

    public static void main(String[] args) throws IOException {

        Socket socket = new Socket("localhost" , 8888);

        System.out.println("客户端启动了!");

        ClientThread clientThread = new ClientThread(socket);
        clientThread.start();

        PrintStream printStream = new PrintStream(socket.getOutputStream());

        // 接收任意时刻服务器发送过来的数据
        BufferedReader bufferedReaderSystemIn = new BufferedReader(new InputStreamReader(System.in));
        String lineSystemIn = null;

        while( (lineSystemIn = bufferedReaderSystemIn.readLine()) != null )
        {
            printStream.println(lineSystemIn);
            printStream.flush();
        }




    }
}

class ClientThread extends Thread{

    private Socket socket;

    public ClientThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

        try {
            // 接收任意时刻服务器发送过来的数据
            BufferedReader bufferedReader = null;

            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line = null;

            while( (line = bufferedReader.readLine()) != null )
            {
                System.out.println("接收到服务器发送过来的数据： " + line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}